package com.hongdee.crm.base.controller;

import com.alibaba.fastjson.JSON;
import com.hongdee.base.entity.ResultEntity;
import com.hongdee.crm.base.entity.TCrmSurveryItem;
import com.hongdee.crm.base.entity.TCrmSurveyGroup;
import com.hongdee.crm.base.service.ITCrmBaseItemService;
import com.hongdee.crm.base.service.ITCrmBaseSurveryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.persistence.Entity;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by dell on 2017/5/10.
 */
@RestController
@RequestMapping("/crm/base")
public class BaseSurveryItemController {
    @Autowired
    private ITCrmBaseItemService itCrmBaseItemService;
    @RequestMapping("/survery/item/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TCrmSurveryItem tCrmSurveryItem) throws Exception {
        System.out.println(tCrmSurveryItem);
        System.out.println(JSON.toJSON(tCrmSurveryItem));
        TCrmSurveryItem group = itCrmBaseItemService.saveOrUpdate(uid,tCrmSurveryItem);
        System.out.println(group);
        ResultEntity res = new ResultEntity();
        System.out.println("11111111111111111111111111111111111111");
        res.setData(group);
        res.setSuccess(true);
        res.setMsg("保存成功！");
        System.out.print(res);
        return res;
    }
    /*
    * 查询
    * */
    @RequestMapping("/survery/item/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        Map map = itCrmBaseItemService.info(id);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }
    /**
     * 停用
     */
    @RequestMapping("/survery/item/stop")
    @ResponseBody
    public ResultEntity stop(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "0") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        itCrmBaseItemService.update(id, "0");
        res.setSuccess(true);
        res.setMsg("停用成功！");
        return res;
    }
    /**
     * 启用
     */

    @RequestMapping("/survery/item/start")
    @ResponseBody
    public ResultEntity start(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestParam(name = "id", defaultValue = "0") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        itCrmBaseItemService.update(id, "1");
        res.setSuccess(true);
        res.setMsg("启动成功！");
        return res;
    }

    /*
    * 分页
    * */
    @RequestMapping("/survery/item/page")
    @ResponseBody
    public Map newPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                       @RequestParam(name = "token", defaultValue = "") String token,
                       @RequestParam(name = "start", defaultValue = "0") String start,
                       @RequestParam(name = "limit", defaultValue = "50") String limit,
                       @RequestParam(name = "id",defaultValue = "")String id,
                       @RequestParam(name = "itemCode",defaultValue = "")String itemCode,
                       @RequestParam(name = "groupCode",defaultValue = "")String groupCode
    ) throws Exception {
        Map map = this.itCrmBaseItemService.newPage(start, limit, id,itemCode,groupCode);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }
    /*
    * 查询分类列表
    * */
    @RequestMapping("/survery/item/leftTree")
    @ResponseBody
    public ResultEntity tree(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token
    ) throws Exception {
        ResultEntity res = new ResultEntity();

        List dataList = this.itCrmBaseItemService.tree(uid);
        res.setDataList(dataList);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }
}
